package opensocial4gwt;

import com.google.gwt.core.client.JavaScriptObject;

public final class Activity extends JavaScriptObject {
	public static enum Field {
		APP_ID, BODY, BODY_ID, EXTERNAL_ID, ID, MEDIA_ITEMS, POSTED_TIME, PRIORITY, STREAM_FAVICON_URL, STREAM_SOURCE_URL, STREAM_TITLE, STREAM_URL, TEMPLATE_PARAMS, TITLE, TITLE_ID, URL, USER_ID;

		public String value() {
			return value(name());
		}

		private native static String value(String name) /*-{
			return $wnd.opensocial.Activity.Field[name];
		}-*/;
	}

	protected Activity() {
	}

	public String getBody() {
		return getString(Field.BODY.value());
	}

	public void setBody(String body) {
		set(Field.BODY.value(), body);
	}

	public String getBodyId() {
		return getString(Field.BODY_ID.value());
	}

	public void setBodyId(String bodyId) {
		set(Field.BODY_ID.value(), bodyId);
	}

	public String getExternalId() {
		return getString(Field.EXTERNAL_ID.value());
	}

	public void setExternalId(String externalId) {
		set(Field.EXTERNAL_ID.value(), externalId);
	}

	public Integer getPriority() {
		return getInt(Field.PRIORITY.value());
	}

	public void setPriority(Integer priority) {
		set(Field.PRIORITY.value(), priority);
	}

	public String getStreamFavIconURL() {
		return getString(Field.STREAM_FAVICON_URL.value());
	}

	public void setStreamFavIconURL(String streamFavIconURL) {
		set(Field.STREAM_FAVICON_URL.value(), streamFavIconURL);
	}

	public String getStreamSourceURL() {
		return getString(Field.STREAM_SOURCE_URL.value());
	}

	public void setStreamSourceURL(String streamSourceURL) {
		set(Field.STREAM_SOURCE_URL.value(), streamSourceURL);
	}

	public String getStreamTitle() {
		return getString(Field.STREAM_TITLE.value());
	}

	public void setStreamTitle(String streamTitle) {
		set(Field.STREAM_TITLE.value(), streamTitle);
	}

	public String getStreamURL() {
		return getString(Field.STREAM_URL.value());
	}

	public void setStreamURL(String streamURL) {
		set(Field.STREAM_URL.value(), streamURL);
	}

	public String getTitle() {
		return getString(Field.TITLE.value());
	}

	public void setTitle(String title) {
		set(Field.TITLE.value(), title);
	}

	public String getTitleId() {
		return getString(Field.TITLE_ID.value());
	}

	public void setTitleId(String titleId) {
		set(Field.TITLE_ID.value(), titleId);
	}

	public String getURL() {
		return getString(Field.URL.value());
	}

	public void setURL(String url) {
		set(Field.URL.value(), url);
	}

	public String getAppId() {
		return getString(Field.APP_ID.value());
	}

	public String getId() {
		return getString(Field.ID.value());
	}

	public Long getPostedTime() {
		String s = getString(Field.POSTED_TIME.value());
		if (s == null)
			return null;
		return new Long(s);
	}

	public String getUserId() {
		return getString(Field.USER_ID.value());
	}

	// ---- TEMPLATE PARAMS ----

	private final JsObject getTemplateParams() {
		return getJsObject(Field.TEMPLATE_PARAMS.value());
	}

	private final void setTemplateParams(JsObject templateParams) {
		set(Field.TEMPLATE_PARAMS.value(), templateParams);
	}

	public void putTemplateParam(String key, String value) {
		JsObject o = getTemplateParams();
		if (o == null) {
			o = JsObject.newObject();
			setTemplateParams(o);
		}
		o.set(key, value);
	}

	public String getTemplateParam(String key) {
		JsObject o = getTemplateParams();
		if (o == null)
			return null;
		else
			return o.getString(key);
	}

	public void removeTemplateParam(String key) {
		JsObject o = getTemplateParams();
		if (o != null)
			o.remove(key);
	}

	public String[] getTemplateParamKeys() {
		JsObject o = getTemplateParams();
		if (o == null)
			return new String[0];
		else
			return o.getKeys();
	}

	// ---- MEDIA ITEMS ----

	public void addMediaItem(MediaItem mediaItem) {
		JsObject o = getMediaItems();
		if (o == null) {
			o = JsObject.newArray();
			setMediaItems(o);
		}
		o.push(mediaItem);
	}

	public MediaItem getMediaItem(int index) {
		JsObject o = getMediaItems();
		if (o != null) {
			return o.get(index);
		} else {
			return null;
		}
	}

	public int getMediaItemCount() {
		JsObject o = getMediaItems();
		if (o == null)
			return 0;
		else
			return o.length();
	}

	public void removeMediaItem(int index) {
		JsObject o = getMediaItems();
		if (o != null) {
			o.remove(index);
		}
	}

	private final JsObject getMediaItems() {
		return getJsObject(Field.MEDIA_ITEMS.value());
	}

	private final void setMediaItems(JsObject templateParams) {
		set(Field.MEDIA_ITEMS.value(), templateParams);
	}

	private final native String getString(String field) /*-{
		return this.getField(field);
	}-*/;

	private final native Integer getInt(String field) /*-{
		return this.getField(field);
	}-*/;

	private final native JsObject getJsObject(String field) /*-{
		return this.getField(field);
	}-*/;

	private final native void set(String field, Object value) /*-{
		this.setField(field, value);
	}-*/;

}
